package com.hzjh.base.style.view.bottomtab

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.hzjh.base.style.R
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

class SpecialTab @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseTabItem(context, attrs, defStyleAttr) {
    companion object{
        fun newItem(context: Context,drawable: Int, checkedDrawable: Int, text: String): SpecialTab {
            val normalItemView = SpecialTab(context)
            normalItemView.initialize(drawable, checkedDrawable, text)
            normalItemView.setTextDefaultColor(ContextCompat.getColor(context, R.color.coreBlack))
            normalItemView.setTextCheckedColor(ContextCompat.getColor(context, R.color.coreBlue))
            return normalItemView
        }
    }


    private var mIcon: ImageView? = null
    private var mTitle: TextView? = null
    private var flContentBottom:FrameLayout?=null
    private var mMessages: RoundMessageView? = null

    private var mDefaultDrawable: Drawable? = null
    private var mCheckedDrawable: Drawable? = null

    private var mDefaultTextColor = 0x56000000
    private var mCheckedTextColor = 0x56000000

    private var mChecked = false


    init {
        LayoutInflater.from(context).inflate(R.layout.special_tab, this, true)
        mIcon = findViewById(R.id.icon)
        mTitle = findViewById(R.id.title)
        mMessages = findViewById(R.id.messages)
        flContentBottom = findViewById(R.id.fl_content_bottom)
    }

    override fun setOnClickListener(l: OnClickListener?) {
        val view = getChildAt(0)
        view?.setOnClickListener(l)
    }

    /**
     * 方便初始化的方法
     *
     * @param drawableRes        默认状态的图标
     * @param checkedDrawableRes 选中状态的图标
     * @param title              标题
     */
    open fun initialize(
        @DrawableRes drawableRes: Int,
        @DrawableRes checkedDrawableRes: Int,
        title: String?
    ) {
        mDefaultDrawable = ContextCompat.getDrawable(context, drawableRes)
        mCheckedDrawable = ContextCompat.getDrawable(context, checkedDrawableRes)
        mTitle!!.text = title
    }

    override fun setChecked(checked: Boolean) {
        if (checked) {
            mIcon!!.setImageDrawable(mCheckedDrawable)
            mTitle!!.setTextColor(mCheckedTextColor)
        } else {
            mIcon!!.setImageDrawable(mDefaultDrawable)
            mTitle!!.setTextColor(mDefaultTextColor)
        }
        mChecked = checked
    }

    override fun setMessageNumber(number: Int) {
        mMessages!!.messageNumber = number
    }

    override fun setHasMessage(hasMessage: Boolean) {
        mMessages!!.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String?) {
        mTitle!!.text = title
    }

    override fun setDefaultDrawable(drawable: Drawable?) {
        mDefaultDrawable = drawable
        if (!mChecked) {
            mIcon!!.setImageDrawable(drawable)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable?) {
        mCheckedDrawable = drawable
        if (mChecked) {
            mIcon!!.setImageDrawable(drawable)
        }
    }

    override fun getTitle(): String? {
        return mTitle!!.text.toString()
    }

    open fun setTextDefaultColor(@ColorInt color: Int) {
        mDefaultTextColor = color
    }

    open fun setTextCheckedColor(@ColorInt color: Int) {
        mCheckedTextColor = color
    }

    fun setTabBackground(color: Int){
        flContentBottom?.setBackgroundColor(color)
    }

    fun setTabTextColor(color: Int){
        mTitle?.setTextColor(color)
    }

    fun setTabImageResource(resId: Int){
        mIcon?.setImageResource(resId)
    }
}